﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Tunneler {
    class PlayerInfo {
        Texture2D backgroundTexture;
        Vector2 backgroundPos;

        Texture2D shieldTexture;
        Vector2 shieldPos;
        Texture2D healthTexture;
        Vector2 healthPos;
        Texture2D energyTexture;
        Vector2 energyPos;
        SpriteFont livesSprite;
        Vector2 livesPos;

        Vector2 powerUpPos;
        Texture2D hasteTexture;
        Texture2D slowTexture;
        Texture2D leakTexture;
        Texture2D rapidFireTexture;
        Texture2D disableTexture;

        Player player;

        int width;
        int height;

        float barWidth;
        float barHeight;

        int powerUpWidth;
        int powerUpHeight;

        float backgroundScale;
        float shieldScale;
        float energyScale;

        public PlayerInfo(Player player, Vector2 position, int width, int height) {
            this.player = player;
            this.backgroundPos = position;

            this.width = width;
            this.height = height;

        }

        public void LoadContent(ContentManager content) {
            backgroundTexture = content.Load<Texture2D>("uiTexture");
            shieldTexture = content.Load<Texture2D>("shieldBar");
            healthTexture = content.Load<Texture2D>("healthBar");
            energyTexture = content.Load<Texture2D>("energyBar");
            livesSprite = content.Load<SpriteFont>("font");

            hasteTexture = content.Load<Texture2D>("haste");
            slowTexture = content.Load<Texture2D>("slow");
            rapidFireTexture = content.Load<Texture2D>("rapidFire");
            leakTexture = content.Load<Texture2D>("leak");
            disableTexture = content.Load<Texture2D>("disable");

            backgroundScale = (float)width / (float)backgroundTexture.Width;

            int barOffsetX = width / 8;
            int barOffsetY = height / 8;

            barWidth = (float)width / 2f;
            barHeight = height / 5;

            powerUpHeight = 2 * (int)barHeight;
            //powerUpWidth = (int)barWidth / 2;
            powerUpWidth = powerUpHeight;

            shieldScale = (float)barWidth / (float)shieldTexture.Width;
            energyScale = (float)barWidth / (float)energyTexture.Width;


            shieldPos = new Vector2(backgroundPos.X + barOffsetX, backgroundPos.Y + barOffsetY);
            healthPos = new Vector2(backgroundPos.X + barOffsetX, backgroundPos.Y + barOffsetY + shieldTexture.Height * shieldScale);
            energyPos = new Vector2(backgroundPos.X + barOffsetX, backgroundPos.Y + barOffsetY + 2 * shieldTexture.Height * shieldScale);
            livesPos = new Vector2(energyPos.X + energyTexture.Width + 10, energyPos.Y);
            powerUpPos = new Vector2(shieldPos.X + (int)(barWidth*1.1), shieldPos.Y);
        }

        public void Draw(SpriteBatch spriteBatch) {
            // Draw Background
            spriteBatch.Draw(backgroundTexture, new Rectangle((int)backgroundPos.X, (int)backgroundPos.Y, width, height), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0f);

            // Draw Shield
            Rectangle shSrc = new Rectangle(0, 0, (int)(shieldTexture.Width * ((float)player.Shield / 100f)), (int)shieldTexture.Height);
            Rectangle shDest = new Rectangle((int)shieldPos.X, (int)shieldPos.Y, (int)(barWidth * player.Shield / 100f), (int)barHeight);
            spriteBatch.Draw(shieldTexture, shDest, shSrc, Color.White);

            // Draw Health
            Rectangle hpSrc = new Rectangle(0, 0, (int)(healthTexture.Width * ((float)player.Health / 100f)), (int)healthTexture.Height);
            Rectangle hpDest = new Rectangle((int)healthPos.X, (int)healthPos.Y, (int)(barWidth * player.Health / 100f), (int)barHeight);
            spriteBatch.Draw(healthTexture, hpDest, hpSrc, Color.White);

            // Draw Energy
            Rectangle enSrc = new Rectangle(0, 0, (int)(energyTexture.Width * ((float)player.Energy / 100f)), (int)shieldTexture.Height);
            Rectangle enDest = new Rectangle((int)energyPos.X, (int)energyPos.Y, (int)(barWidth * player.Energy / 100f), (int)barHeight);
            spriteBatch.Draw(energyTexture, enDest, enSrc, Color.White);
            spriteBatch.DrawString(livesSprite, String.Format("Lives: {0}", player.RemainingLives), livesPos, Color.Black);

            // Draw Power Up
            if (player.PowerUp != null
                && !(player.PowerUp is Heal)
                && !(player.PowerUp is Energy)) {
                Texture2D powerUpTexture = GetPowerUpTexture();
                Rectangle destRec = new Rectangle((int)powerUpPos.X, (int)powerUpPos.Y,
                    powerUpWidth, powerUpHeight);
                spriteBatch.Draw(powerUpTexture, destRec, null, Color.White);
            }
        }

        private Texture2D GetPowerUpTexture() {
            if (player.PowerUp is Haste) {
                return hasteTexture;
            }
            if (player.PowerUp is Slow) {
                return slowTexture;
            }
            if (player.PowerUp is RapidFire) {
                return rapidFireTexture;
            }
            if (player.PowerUp is Disable) {
                return disableTexture;
            }
            if (player.PowerUp is Leak) {
                return leakTexture;
            }
            return null;
        }
    }
}
